package com.inspur.jxpcxt.activitys;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.inspur.jxpcxt.app.JXPCApplication;
import com.inspur.jxpcxt.bean.StudentCourse;
import com.inspur.jxpcxt.service.BackService;
import com.inspur.jxpcxt.service.HomeCommitService;
import com.inspur.jxpcxt.utils.JXPCLog;
import com.inspur.jxpcxt.utils.NetworkUtils;
import com.inspur.jxpcxt.widget.ExitDialog;
import com.inspur.jypcxt.R;
/**
 * 评测页面
 * @author Administrator
 *
 */
public class HomeActivity extends FragmentActivity implements OnClickListener {
	//课程下拉框
	private Spinner mCourseSp = null;
	//教员下拉框
	private Spinner mTnameSp = null;
	//自己的学号
	private TextView mSnoTv = null;
	//自己的姓名
	private TextView mSnameTv = null;
	//用于存储从服务器返回的选课信息
	List<StudentCourse> mScList = new ArrayList<StudentCourse>();
	//退出按钮
	private Button mExitBtn = null;
	//提交按钮
	private Button mRegisterBtn = null;
	//10个评分的编辑框
	EditText mGoalsRequestedEt = null;
	EditText mPrepareLessonsTeachEt = null;
	EditText mContentOfCoursesEt = null;
	EditText mTeachWayEt = null;
	EditText mAbilityTrainEt = null;
	EditText mTeachingMmannerMeterEt = null;
	EditText mLanguageExpressionEt = null;
	EditText mClassroomDisciplineEt = null;
	EditText mBlackboardDesignEt = null;
	EditText mTeachingResultEt = null;
	
	
	private String tname = "";
	private String course = "";
	private float avg;
	private String grade = "";
	private String tno = "";
	
	//用于刷新UI
			//Handler mHandler = null;
@SuppressLint("HandlerLeak")
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(@Nullable Bundle arg0)
	{
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_home);
		JXPCApplication.getInstance().addActivity(HomeActivity.this);
		initUI();
		Intent intent = getIntent();
		 mScList = (ArrayList)intent.getParcelableArrayListExtra("sc");
		 
		 //存储所选课程数组
		 List<String> courseList = new ArrayList<String>();
		 //存储课程老师的数组
		 List<String> tnameList = new ArrayList<String>();
		 if(mScList == null || mScList.size() == 0){
			 return;
		 }
		 //将评测学生的学号显示在评测页面
		mSnoTv.setText(mScList.get(0).getSno());
		//将评测学生的姓名显示在评测页面
		mSnameTv.setText(mScList.get(0).getSname());
		//获得课程老师的编号
		//tno = mScList.get(0).getTno();
		
		
		for(int i=0;i<mScList.size();i++){
			tnameList.add(mScList.get(i).getTname());
			courseList.add(mScList.get(i).getCname());
		}
		
//		 mHandler = new Handler(){
//				@Override
//				public void handleMessage(Message msg)
//					{
//						if(msg.what == Constants.REFRESH_UI){
//							refresh();
//						}
//					}
//		};
		
		//将服务器端数据库中的课程部分放入数组中
		//创建ArrayAdapter对象
		ArrayAdapter<String> tnameAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item, tnameList);
		ArrayAdapter<String> courseAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item, courseList);
		
		mTnameSp.setAdapter(tnameAdapter);
//		mTnameSp.setPrompt("请选择老师");
		mCourseSp.setAdapter(courseAdapter);
//		mCourseSp.setPrompt("请选择课程");
		
		//选择教师响应的事件
		mTnameSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapterView, View arg1, int arg2, long arg3)
				{
//					 Spinner spinner1 = (Spinner) adapterView;  
//		                String content = (String) spinner1.getItemAtPosition(i);
					//取得与Spinner关联的列表数据的指定位置的数据</span>  
					tname = (String) adapterView.getItemAtPosition(arg2);
					//获得课程老师的编号
					tno = mScList.get(arg2).getTno();
					JXPCLog.e("tno", tno);
					Toast.makeText(HomeActivity.this, "您点击了："+tname, Toast.LENGTH_LONG).show();
				}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
				{
					
				}
		});
		//选择课程响应的事件
	mCourseSp.setOnItemSelectedListener(new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> adapterView, View arg1, int arg2, long arg3)
			{
				//取得与Spinner关联的列表数据的指定位置的数据</span>  
				course = (String) adapterView.getItemAtPosition(arg2);
				Toast.makeText(HomeActivity.this, "您点击了："+course, Toast.LENGTH_LONG).show();
			}

		@Override
		public void onNothingSelected(AdapterView<?> arg0)
			{
				
			}
	});
		
	}
private void initUI()
	{
		// TODO Auto-generated method stub
		mExitBtn = (Button) findViewById(R.id.btn_exit);
		mCourseSp = (Spinner) findViewById(R.id.sp_course);
		mTnameSp = (Spinner) findViewById(R.id.sp_teacher);
		mSnoTv = (TextView) findViewById(R.id.tv_sno);
		mSnameTv = (TextView) findViewById(R.id.tv_sname);
		
		//评分
		mGoalsRequestedEt = (EditText) findViewById(R.id.et_home_goals_requested);
		mPrepareLessonsTeachEt = (EditText) findViewById(R.id.et_home_prepare_lessons_teach);
		mContentOfCoursesEt = (EditText) findViewById(R.id.et_home_content_of_courses);
		mTeachWayEt = (EditText) findViewById(R.id.et_home_teach_way);
		mAbilityTrainEt = (EditText) findViewById(R.id.et_home_ability_train);
		mTeachingMmannerMeterEt = (EditText) findViewById(R.id.et_home_teaching_manner_meter);
		mLanguageExpressionEt = (EditText) findViewById(R.id.et_home_language_expression);
		mClassroomDisciplineEt = (EditText) findViewById(R.id.et_home_classroom_discipline);
		mBlackboardDesignEt = (EditText) findViewById(R.id.et_home_blackboard_design);
		mTeachingResultEt = (EditText) findViewById(R.id.et_home_teaching_result);
		
		mRegisterBtn = (Button) findViewById(R.id.btn_submit);
		
		mExitBtn.setOnClickListener(this);
		mRegisterBtn.setOnClickListener(this);
		
	}
@Override
public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_exit:{
			//跳出一个dialog
			HomeActivity.this.finish();
		}
			break;
		case R.id.btn_submit:{
			//提交按钮响应的事件
			commitEvaluationResults();
		}
		break;
		default:
			break;
		}
		
	}
private void commitEvaluationResults()
	{
		 
		
		String s0 = mGoalsRequestedEt.getText().toString().trim();
		String s1 = mPrepareLessonsTeachEt.getText().toString().trim();
		String s2 = mContentOfCoursesEt.getText().toString().trim();
		String s3 = mTeachWayEt.getText().toString().trim();
		String s4 = mAbilityTrainEt.getText().toString().trim();
		String s5 = mTeachingMmannerMeterEt.getText().toString().trim();
		String s6 = mLanguageExpressionEt.getText().toString().trim();
		String s7 = mClassroomDisciplineEt.getText().toString().trim();
		String s8 = mBlackboardDesignEt.getText().toString().trim();
		String s9 = mTeachingResultEt.getText().toString().trim();
		
		if(TextUtils.isEmpty(s0) || TextUtils.isEmpty(s1) || TextUtils.isEmpty(s2) || TextUtils.isEmpty(s3) 
				|| TextUtils.isEmpty(s4) || TextUtils.isEmpty(s5) || TextUtils.isEmpty(s6) ||TextUtils.isEmpty(s7) || TextUtils.isEmpty(s8) || TextUtils.isEmpty(s9)){
			Toast.makeText(HomeActivity.this, "评分项不可为空", Toast.LENGTH_LONG).show();
			return;
		}
		if(!NetworkUtils.isConnectInternet(HomeActivity.this)){
			Toast.makeText(HomeActivity.this, "网络不可用，请检查网络连接", Toast.LENGTH_SHORT).show();
			return;
		}
		float num0 = Float.parseFloat(mGoalsRequestedEt.getText().toString().trim());
		float num1 = Float.parseFloat(mPrepareLessonsTeachEt.getText().toString().trim());
		float num2 = Float.parseFloat(mContentOfCoursesEt.getText().toString().trim());
		float num3 = Float.parseFloat(mTeachWayEt.getText().toString().trim());
		float num4 = Float.parseFloat(mAbilityTrainEt.getText().toString().trim());
		float num5 = Float.parseFloat(mTeachingMmannerMeterEt.getText().toString().trim());
		float num6 = Float.parseFloat(mLanguageExpressionEt.getText().toString().trim());
		float num7 = Float.parseFloat(mClassroomDisciplineEt.getText().toString().trim());
		float num8 = Float.parseFloat(mBlackboardDesignEt.getText().toString().trim());
		float num9 = Float.parseFloat(mTeachingResultEt.getText().toString().trim());
		
		if(num8 == 0){
			avg = (num0 + num1 + num2 + num3 + num4 + num5 + num6 + num7 + num9)/9 ;
		}else{
			avg = (num0 + num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9)/10 ;
		}
		if(avg >=90){
			grade = "优秀";
		}
		if(80<=avg && avg <90){
			grade = "良好";
		}
		if(60<=avg && avg <80){
			grade = "及格";
		}
		if(avg<60){
			grade = "不及格";
		}
		
		new Thread(){
				public void run() {
						final String result = HomeCommitService.HomeCommitByClientGet(tname, course, mScList.get(0).getSno(),mScList.get(0).getSname(), avg, grade,tno);
							if(result == null){
								return;
							}
							JXPCLog.e("homeActivity", result);
							if(result.equals("评测成功")){
								runOnUiThread(new  Runnable() {
									public void run()
										{
											Toast.makeText(HomeActivity.this,result , Toast.LENGTH_LONG).show();
											//mHandler.sendEmptyMessageDelayed(Constants.REFRESH_UI, 2000);
											cleanData();
										}
								});
							}else{
								runOnUiThread(new  Runnable() {
									public void run()
										{
											Toast.makeText(HomeActivity.this,"请求失败……" , Toast.LENGTH_LONG).show();
										}
								});
							}
				};
		}.start();
	}
private void cleanData(){
		mGoalsRequestedEt.setText("");
		mPrepareLessonsTeachEt.setText("");
		mContentOfCoursesEt.setText("");
		mTeachWayEt.setText("");
		mAbilityTrainEt.setText("");
		mTeachingMmannerMeterEt.setText("");
		mLanguageExpressionEt.setText("");
		mClassroomDisciplineEt.setText("");
		mBlackboardDesignEt.setText("");
		mTeachingResultEt.setText("");
}
/**
 * 刷新页面
 */
public void refresh(){
		HomeActivity.this.finish();
		Intent intent = new Intent(HomeActivity.this, HomeActivity.class);
		startActivity(intent);
}
/**
 * 点击两次退出程序
 */
@Override
public void onBackPressed()
	{
		// TODO Auto-generated method stub
		ExitDialog.showExit(HomeActivity.this);
		//
	}
@Override
protected void onDestroy()
	{
		super.onDestroy();
		//处理程序销毁之后将登陆已登录标志置为false
		new Thread() {
				public void run()
					{
						// 将logined置为no
					BackService.BackByClientGet(mScList.get(0).getSno());
						
						}
		}.start();
	}

}
